<template>
    <div :id="message.id" class="sc-message-container">
        <div class="time">
            <slot name="time"></slot>
        </div>
        <div
            class="sc-message--content"
            :class="{
                sent: message.author === 'me',
                received: message.author !== 'me' && message.type !== 'system',
                system: message.type === 'system',
            }"
        >
            <div class="user-avatar" v-if="message.author !== 'me' && message.type !== 'system'">
                <img :src="message.avatar" alt="" class="avatar" />
            </div>

            <div class="message">
                <div class="name" v-if="message.author !== 'me' && message.type !== 'system'">
                    {{ message.name }}
                </div>

                <SystemMessage
                    v-if="message.type === 'system'"
                    :data="message"
                    @reConnect="emits('reConnect')"
                >
                </SystemMessage>

                <ReplyMessage
                    v-else-if="message.type === 'reply'"
                    :message="message"
                    @replyClick="(val) => emits('replyClick', val)"
                    @refresh="(val) => emits('refresh', val)"
                ></ReplyMessage>

                <div class="message-content" v-else>
                    <template v-if="message.author === 'me'">
                        <el-icon class="is-loading icon" v-if="message.status === 'loading'">
                            <Loading />
                        </el-icon>

                        <img
                            class="img"
                            src="@/assets/fail.png"
                            alt=""
                            v-if="message.status === 'fail'"
                        />
                    </template>

                    <TextMessage
                        v-if="message.type === 'text'"
                        :message="message"
                        @message-click="(btnType) => emits('messageClick', btnType)"
                        @tabs-click="(id) => emits('tabsClick', id)"
                    >
                    </TextMessage>

                    <EmojiMessage v-else-if="message.type === 'emoji'" :data="message.data" />

                    <FileMessage
                        v-else-if="message.type === 'file'"
                        :data="message.data"
                        @download="(file) => emits('download', file)"
                    />
                </div>

                <slot name="system-message-body" :message="message.data"> </slot>
            </div>

            <img
                v-if="message.author === 'me' && message.type !== 'system'"
                :src="message.avatar"
                alt=""
                class="sent-avatar"
            />
        </div>
    </div>
</template>

<script setup>
import TextMessage from './messages/TextMessage.vue'
import FileMessage from './messages/FileMessage.vue'
import EmojiMessage from './messages/EmojiMessage.vue'
import SystemMessage from './messages/SystemMessage.vue'
import ReplyMessage from './messages/ReplyMessage.vue'
import { Loading } from '@element-plus/icons-vue'

const props = defineProps({
    message: {
        type: Object,
        required: true,
    },
})

const emits = defineEmits([
    'download',
    'messageClick',
    'tabsClick',
    'reConnect',
    'replyClick',
    'refresh',
])
</script>

<style lang="scss">
/* TODO: re-org and scope this style block */

.sc-message-container {
    margin-bottom: 20px;
    width: 100%;

    .time {
        width: 100%;
        text-align: center;
        font-size: 14px;
        color: #999999;
        margin-bottom: 22px;
    }
}

.sc-message--content {
    width: 100%;
    display: flex;

    .user-avatar {
        width: 40px;
        height: 40px;
        margin-right: 10px;
        // border: 0.5px solid #e6e6e6;
        border-radius: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
        .avatar {
            width: 40px;
            height: 40px;
        }
    }

    .sent-avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        margin-left: 10px;
    }

    .message {
        display: flex;
        flex-direction: column;
        max-width: 434px;

        .message-content {
            display: flex;
            align-items: flex-end;
            .icon {
                margin-right: 4px;
            }

            .img {
                width: 16px;
                height: 16px;
                margin-right: 4px;
            }
        }
    }
    .name {
        color: #999999;
        font-size: 14px;
        margin-bottom: 6px;
    }
}

.sc-message--content.sent {
    justify-content: flex-end;

    .message {
        align-items: flex-end;
        justify-content: flex-end;
    }
}

.sc-message--content.system {
    justify-content: center;
}
</style>
